﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace PointCloudExporter
{ 
    public class ViewManipunateBehavior : MonoBehaviour
    {
		private Vector3 GetBaseInput()
		{ //returns the basic values, if it's 0 than it's not active.
			Vector3 p_Velocity = new Vector3();
			if (Input.mouseScrollDelta.y != 0)
			{

				float fy = (Input.mouseScrollDelta.y * viewDepth / 3);
				p_Velocity += new Vector3(0, 0, fy);
			}
			if (Input.GetMouseButton(2))
			{
				p_Velocity += new Vector3(-Input.GetAxis("Mouse X"), -Input.GetAxis("Mouse Y"), 0) * viewDepth / 3;

			}
			if (Input.GetKey(KeyCode.W))
			{
				p_Velocity += new Vector3(0, 1, 0);
			}
			if (Input.GetKey(KeyCode.S))
			{
				p_Velocity += new Vector3(0, -1, 0);
			}
			if (Input.GetKey(KeyCode.A))
			{
				p_Velocity += new Vector3(-1, 0, 0);
			}
			if (Input.GetKey(KeyCode.D))
			{
				p_Velocity += new Vector3(1, 0, 0);
			}
			return p_Velocity;
		}

		public Camera cameraObj;
		public float viewDepth = 3;
        //public GameObject myGameObj;

        public void Awake()
        {
			if (cameraObj == null) cameraObj = Camera.main;

		}

		void Update()
		{
			UpdateCamera();
		}
		public void UpdateCamera()
		{
			float rspeed = 5f;

			Vector3 viewFront = new Vector3(0, 0, viewDepth);
			Vector3 viewCenter = cameraObj.transform.position + cameraObj.transform.rotation * viewFront;
			if (Input.GetMouseButton(1))
			{
				//cameraObj.transform.position += myGameObj.transform.position - cameraObj.transform.position;
				cameraObj.transform.RotateAround(viewCenter,
												cameraObj.transform.up,
												Input.GetAxis("Mouse X") * rspeed);

				cameraObj.transform.RotateAround(viewCenter,
												cameraObj.transform.right,
												-Input.GetAxis("Mouse Y") * rspeed);
			}

			Vector3 d = GetBaseInput();
			if (!d.Equals(new Vector3(0, 0, 0)))
			{
				cameraObj.transform.position += cameraObj.transform.rotation * d;

			}
		}
	}
}
